package com.jiuair.cloud.data.revenue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jiuair.cloud.data.revenue.entity.UsrUser;
import com.jiuair.cloud.data.revenue.result.PageParams;
import com.jiuair.cloud.data.revenue.result.Result;
import com.jiuair.cloud.data.revenue.service.UsrUserService;
import com.jiuair.cloud.data.revenue.vo.LoginUserDTO;
import com.jiuair.cloud.data.revenue.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.util.*;

/**
 * 用户信息表 前端控制器
 *
 * @author deng
 * @date 2020-08-27
 */
@Api(value = "用户信息表", tags = "用户信息表")
    @RestController
    @RequestMapping("usrUser")
    public class UsrUserController {

    @Autowired
    private UsrUserService targetService;



    /**
     * @description 用户登录
     * @param loginUserDTO
     * @return
     */
    @PostMapping("/user/login")
    public ResponseVO login(@RequestBody LoginUserDTO loginUserDTO) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return targetService.login(loginUserDTO);
    }



    /**
    * 获取分页数据
    *
    * @return
    */
    @ApiOperation(value = "获取分页数据-用户信息表", notes = "获取分页数据")
    @GetMapping(value = "/list")
    public Result<IPage<UsrUser>> list(@RequestParam(required = false) Map map){
        PageParams pageParams = new PageParams(map);
    //    UsrUser query = pageParams.mapToObject(UsrUser.class);
        QueryWrapper<UsrUser> queryWrapper = new QueryWrapper();
        return Result.ok().data(targetService.page(pageParams,queryWrapper));
    }

    /**
     * 根据ID查找数据
     */
    @ApiOperation(value = "根据ID查找数据-用户信息表", notes = "根据ID查找数据")
    @ResponseBody
    @RequestMapping("/get")
    public Result<UsrUser> get(@RequestParam("id") Long id){
        UsrUser entity = targetService.getById(id);
        return Result.ok().data(entity);
    }

    /**
    * 添加数据
    * @return
    */
    @ApiOperation(value = "添加数据-用户信息表", notes = "添加数据")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "userName", required = true, value = "用户名", paramType = "form"),
         @ApiImplicitParam(name = "passwd", required = true, value = "原密码", paramType = "form"),
         @ApiImplicitParam(name = "mobile", required = true, value = "电话", paramType = "form"),
         @ApiImplicitParam(name = "email", required = true, value = "邮箱", paramType = "form"),
         @ApiImplicitParam(name = "createTime", required = true, value = "创建日期", paramType = "form"),
         @ApiImplicitParam(name = "status", required = true, value = "状态", paramType = "form"),
         @ApiImplicitParam(name = "channelId", required = true, value = "", paramType = "form"),
         @ApiImplicitParam(name = "type", required = true, value = "类型", paramType = "form"),
        @ApiImplicitParam(name = "password", required = true, value = "新密码", paramType = "form")
            })
    @PostMapping("/add")
    public Result add(
        @RequestParam(value = "userName") String userName,
        @RequestParam(value = "passwd") String passwd,
        @RequestParam(value = "mobile") String mobile,
        @RequestParam(value = "email") String email,
        @RequestParam(value = "createTime") LocalDate createTime,
        @RequestParam(value = "status") String status,
        @RequestParam(value = "channelId") Long channelId,
        @RequestParam(value = "type") String type,
        @RequestParam(value = "password") String password
            ){
        UsrUser entity = new UsrUser();
        entity.setUserName(userName);
        entity.setPasswd(passwd);
        entity.setMobile(mobile);
        entity.setEmail(email);
        entity.setCreateTime(createTime);
        entity.setStatus(status);
        entity.setChannelId(channelId);
        entity.setType(type);
        entity.setPassword(password);
        targetService.save(entity);
        return Result.ok();
    }

    /**
    * 新增用户信息表数据
    * @return
    */
    @ApiOperation(value = "保存数据-用户信息表", notes = "保存数据")
    @PostMapping("/save")
    @ResponseBody
    public Result add(@RequestBody UsrUser usrUser){
        Result Result=new Result();
        targetService.save(usrUser);
        return Result.ok();
        }


    /**
    * 更新数据
    * @return
    */
    @ApiOperation(value = "更新数据-用户信息表", notes = "更新数据")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "id", required = true, value = "主键id", paramType = "form"),
                    @ApiImplicitParam(name = "userName", required = true, value = "用户名", paramType = "form"),
                    @ApiImplicitParam(name = "passwd", required = true, value = "原密码", paramType = "form"),
                    @ApiImplicitParam(name = "mobile", required = true, value = "电话", paramType = "form"),
                    @ApiImplicitParam(name = "email", required = true, value = "邮箱", paramType = "form"),
                    @ApiImplicitParam(name = "createTime", required = true, value = "创建日期", paramType = "form"),
                    @ApiImplicitParam(name = "status", required = true, value = "状态", paramType = "form"),
                    @ApiImplicitParam(name = "channelId", required = true, value = "", paramType = "form"),
                    @ApiImplicitParam(name = "type", required = true, value = "类型", paramType = "form"),
                    @ApiImplicitParam(name = "password", required = true, value = "新密码", paramType = "form")
        })
        @PostMapping("/update")
        public Result update(
                @RequestParam(value = "id") Integer id,
                @RequestParam(value = "userName") String userName,
                @RequestParam(value = "passwd") String passwd,
                @RequestParam(value = "mobile") String mobile,
                @RequestParam(value = "email") String email,
                @RequestParam(value = "createTime") LocalDate createTime,
                @RequestParam(value = "status") String status,
                @RequestParam(value = "channelId") Long channelId,
                @RequestParam(value = "type") String type,
                @RequestParam(value = "password") String password
        ){
            UsrUser entity = new UsrUser();
                    entity.setId(id);
                    entity.setUserName(userName);
                    entity.setPasswd(passwd);
                    entity.setMobile(mobile);
                    entity.setEmail(email);
                    entity.setCreateTime(createTime);
                    entity.setStatus(status);
                    entity.setChannelId(channelId);
                    entity.setType(type);
                    entity.setPassword(password);
                targetService.updateById(entity);
                return Result.ok();
        }

    /**
    * 删除数据
    * @return
    */
    @ApiOperation(value = "删除数据-用户信息表", notes = "删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "id", paramType = "form")
    })
    @PostMapping("/remove")
    public Result remove(
            @RequestParam(value = "id") Long id
    ){
            targetService.removeById(id);
            return Result.ok();
      }

    /**
    * 批量删除数据
    * @return
    */
    @ApiOperation(value = "批量删除数据-用户信息表", notes = "批量删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", required = true, value = "多个用,号隔开", paramType = "form")
    })
    @PostMapping("/batch/remove")
    public Result batchRemove(
                @RequestParam(value = "ids") String ids
            ){
            targetService.removeByIds(Arrays.asList(ids.split(",")));
            return Result.ok();
     }

}
